package com.chx.three.fxgood;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.chx.three.fxgood.fragments.DynamicFragment;
import com.chx.three.fxgood.fragments.GroupFragment;
import com.chx.three.fxgood.fragments.PublicFragment;
import com.chx.three.fxgood.utils.DensityUtil;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;

/**
 * 主界面导航栏
 */
@ContentView(R.layout.activity_main)
public class MainActivity extends FragmentActivity implements View.OnClickListener {
    @ViewInject(R.id.main_nav_content)
    FrameLayout mContent;
    @ViewInject(R.id.main_nav_menu)
    ImageView mMenu;;
    //动态
    @ViewInject(R.id.main_nav_dynamic)
    LinearLayout mDynamic;
    //饭堂
    @ViewInject(R.id.main_nav_publish)
    LinearLayout mPublish;
    //饭团
    @ViewInject(R.id.main_nav_group)
    LinearLayout mGroup;
    List<Fragment> fragmentList;
    @ViewInject(R.id.main_nav_bottom1)
    View mBottom1;
    @ViewInject(R.id.main_nav_bottom2)
    View mBottom2;
    @ViewInject(R.id.main_nav_bottom3)
    View mBottom3;
    SlidingMenu slidingMenu;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ViewUtils.inject(this);
        fragmentList=new ArrayList<>();
        fragmentList.add(new DynamicFragment());
        fragmentList.add(new PublicFragment());
        fragmentList.add(new GroupFragment());
        mDynamic.setOnClickListener(this);
        mPublish.setOnClickListener(this);
        mGroup.setOnClickListener(this);
        mMenu.setOnClickListener(this);

        android.support.v4.app.FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.main_nav_content, fragmentList.get(0));
        transaction.commit();
        slidingMenu = new SlidingMenu(this, SlidingMenu.SLIDING_CONTENT);
        slidingMenu.setMenu(R.layout.layout_sliding_menu);
        slidingMenu.setMode(SlidingMenu.LEFT);
        DisplayMetrics outMetrics = new DisplayMetrics();
        getWindow().getWindowManager().getDefaultDisplay().getMetrics(outMetrics);
        int widthPixels = outMetrics.widthPixels;
        //屏幕宽度的dip
        int dip = DensityUtil.px2dip(this, widthPixels);
        //250的菜单宽度
        int width=dip-250;
        slidingMenu.setBehindOffset(DensityUtil.dip2px(this, width));
        slidingMenu.setShadowWidth(50);
        slidingMenu.setTouchModeBehind(SlidingMenu.TOUCHMODE_FULLSCREEN);
        slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN);
        Button button=(Button)findViewById(R.id.test_button);
        button.setOnClickListener(this);
    }


    /**
     * 点击事件，用于切换fragment和菜单栏按钮
     * @param v
     */
    @Override
    public void onClick(View v) {
        android.support.v4.app.FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        int id = v.getId();
        switch (id){
            case R.id.main_nav_dynamic:
                removeLine();
                mBottom1.setVisibility(View.VISIBLE);
                transaction.replace(R.id.main_nav_content, fragmentList.get(0));
                break;
            case R.id.main_nav_publish:
                removeLine();
                mBottom2.setVisibility(View.VISIBLE);
                transaction.replace(R.id.main_nav_content, fragmentList.get(1));
                break;
            case R.id.main_nav_group:
                removeLine();
                mBottom3.setVisibility(View.VISIBLE);
                transaction.replace(R.id.main_nav_content, fragmentList.get(2));
                break;
            case R.id.main_nav_menu:
                slidingMenu.showMenu();
                break;
            case R.id.test_button:
                Toast.makeText(this,"打我啊，你打我啊~",Toast.LENGTH_SHORT).show();
                break;

        }
        transaction.commit();
    }

    private void removeLine() {
        mBottom1.setVisibility(View.GONE);
        mBottom2.setVisibility(View.GONE);
        mBottom3.setVisibility(View.GONE);
    }
}
